package com.lions.client.view.weather;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.lions.client.view.popup.MyPopupPanel;

public class WeatherPanel extends AbsolutePanel {

	public WeatherPanel() {
		updateWeather();

		// This is the style that will place the logo in the bottom right corner
		// of the weather panel.

		addStyleName("weatherBack");
	}

	/*
	 * This is the function that will be called to create the information for
	 * the weather.
	 * 
	 * This is done using JSONP or JSON with padding, and uses the
	 * requestbuilder to get the data, i did it this way because I didnt want to
	 * set up a proxy server to retrieve the data.
	 * 
	 * Parameter: None Author: Kyle Fink Information about this function can be
	 * found on the professors tech talks about weather.
	 */
	private void updateWeather() {
		String url = "http://api.wunderground.com/api/5cf41ff0b9404421/conditions/q/55812.json?"; // TAKE

		final JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
		jsonp.setCallbackParam("callback");
		jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>() {
			@Override
			public void onFailure(final Throwable caught) {
				new MyPopupPanel("JSON Failure","JSONP onFailure");
			}

			@Override
			public void onSuccess(JavaScriptObject s) {
				String temp = null, vis = null, wind = null;
				String show = null;
				JSONObject obj = new JSONObject(s);

				JSONValue current = obj.get("current_observation");
				JSONObject jB = (JSONObject) JSONParser.parseLenient(current
						.toString());
				JSONValue tempC = jB.get("temp_c");
				JSONValue visKL = jB.get("visibility_km");
				JSONValue windKPH = jB.get("wind_kph");
				JSONValue icon = jB.get("icon_url");
				if (tempC == null) {
					Window.alert("KEY IS NULL");
				} else {
					show = icon.toString();
					temp = tempC.toString();
					vis = visKL.toString();
					wind = windKPH.toString();
				}
				VerticalPanel vp = new VerticalPanel();
				final Image image = new Image();
				image.setUrl(show);

				Label tempLabel = new Label("The Current Temp is: " + temp
						+ " C");
				Label visLabel = new Label("The Current Visability is: " + vis
						+ " km");
				Label windLabel = new Label("The Current Wind Speed is: "
						+ wind + " KPH");

				tempLabel.addStyleName("weatherText");
				visLabel.addStyleName("weatherText");
				windLabel.addStyleName("weatherText");

				vp.add(tempLabel); // TO VIEW
				vp.add(visLabel);
				vp.add(windLabel);
				vp.setStyleName("weatherpanel");
				add(vp);
			}
		});
	}
}
